<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<input type="text">
<button onclick="f()">平方</button>
<button onclick="plus()">相加</button>
<h1>结果：<span></span></h1>
<script>
    /*得到文本框的值*/
    let i = document.querySelector("input");
    /*得到span*/
    let s = document.querySelector("span");
    function plus() {
        if(isNaN(i.value)){
            s.innerText = "输入错误，请重新输入";
            return;
        }
        /*parseFloat将字符串转为整数或者小数 parseInt（3.8）将字符串转为整数3*/
        s.innerText = parseFloat(i.value)+parseFloat(i.value);
    }
    function f() {

        //当字符串进行-*/运算时会自动转为数值,如果转不成则会转成NaN
        //NaN和任何数值进行运算结果都为NaN
        if(isNaN(i.value)){
            s.innerText = "输入错误，请重新输入";
            return;
        }
        /*修改span的值*/
        s.innerText = i.value*i.value;

    }
</script>
</body>
</html>